package com.auxgroup.smarthome.inner.repo;

import com.auxgroup.smarthome.inner.entity.DeviceInfoEntity;
import com.auxgroup.smarthome.repo.GenericDao;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
public interface DeviceInfoEntityRepo extends GenericDao<DeviceInfoEntity,String> {

    /**
     * 查询设备类型
     * @param modelId
     * @return 新设备还是旧设备[0新设备，1旧设备]
     */
    @Query(value = "select device_type from device_model_info where model_id = ?",nativeQuery = true)
    Integer findDeviceType(String modelId);

    DeviceInfoEntity findByMacAndTrashFalse(String mac);

    DeviceInfoEntity findByDeviceIdAndTrashFalse(String deviceId);

    @Modifying
    @Query(value = "delete from device_info where mac = ? or mac = ? " ,nativeQuery = true)
    @Transactional
    int deleteDevice(String newMac,String oldMac);

    @Query(value = "select * from device_info where mac = ? or mac = ? " ,nativeQuery = true)
    List<DeviceInfoEntity> findDeviceListByMac(String newMac,String oldMac);

    DeviceInfoEntity findByDidAndTrashFalse(String did);

    @Query(value = "SELECT DISTINCT di.productKey FROM DeviceInfoEntity di  WHERE  di.source = 1 AND di.trash = 0 AND  di.remark LIKE ?1" )
    List<String> findByModelItem(String modelItem);

    /**
     * 查询设备的实物图
     * @param did
     * @return 实物图链接
     */
    @Query(value = "select entity_uri from device_model_info where model_id = (select model_id from device_info where did = ?)",nativeQuery = true)
    String findEntityUri(String did);

    /**
     * 统计设备总数
     * @return
     */
    @Query(value = "select count(*) from device_info where source = 1 and is_trash = 0" ,nativeQuery = true)
    Integer countBySql();

    @Query(value = "select * from device_info where source = 1 and is_trash = 0 limit ?,?" ,nativeQuery = true)
    List<DeviceInfoEntity> findDeviceList(Integer skip,Integer limit);
}
